package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class CoachEditor extends AutoTeamDialog
{
    private static final long serialVersionUID = -142007470596164860L;
    
    private JButton changeButton;
    private JButton closeButton;
    private AutoTeamTableModel model;
    
    private JTextField firstnameField;
    private JTextField lastnameField;
    private JComboBox teamBox;
    
    private int selectedRow;
    private String origFirstname;
    private String origLastname;
    private String origTeam;
    
    public CoachEditor( JFrame dialogOwner, AutoTeamTableModel model, int row )
    {
        super( dialogOwner );
        this.setTitle( "Edit Coach" );
        this.setDialogSize( new Dimension( 300, 125 ) );
        
        firstnameField = new JTextField( 25 );
        lastnameField = new JTextField( 25 );
        teamBox = new JComboBox( );
        DataAccessObject dao = new DataAccessObject( );
        List <DBRecord> teamRecs = dao.getAllRecords( "teams" );
        for ( DBRecord rec : teamRecs )
            teamBox.addItem( rec.getFieldValue( "teamName" ) );
        
        JPanel buttonPane = new JPanel( );
        changeButton = new JButton( "Change" );
        closeButton = new JButton( "Cancel" );
        buttonPane.add( changeButton, closeButton );
        setButtonActions( this );
        
        JPanel formPane = new JPanel( new GridLayout( 3, 2 ) );
        JLabel firstnameLabel = new JLabel( "Firstname: " );
        JLabel lastnameLabel = new JLabel( "Lastname" );
        JLabel teamLabel = new JLabel( "Team: " );
        
        formPane.add( firstnameLabel );
        formPane.add( firstnameField );
        formPane.add( lastnameLabel );
        formPane.add( lastnameField );
        formPane.add( teamLabel );
        formPane.add( teamBox );
        
        Container contentPane = getContentPane( );
        contentPane.add( formPane, BorderLayout.CENTER );
        contentPane.add( buttonPane, BorderLayout.SOUTH );
        
        pack( );
        
        this.model = model;
        this.selectedRow = row;
        origTeam = (String) model.getValueAt( row, 1 );
        origFirstname = (String) model.getValueAt( row, 2 );
        origLastname = (String) model.getValueAt( row, 3 );
        
        firstnameField.setText( origFirstname );
        lastnameField.setText( origLastname );
        teamBox.setSelectedItem( origTeam );
        
    }
    
    @ Override
    protected void setButtonActions( final AutoTeamDialog thisDialog )
    {
        changeButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                DataAccessObject dao = new DataAccessObject( );
                ColumnData[] identifiers = { new ColumnData( "id", model.getValueAt( selectedRow, 0 ) ) };
                String team = teamBox.getSelectedItem().toString();
                ColumnData col = new ColumnData("teamName", team);
                DBRecord rec = dao.getRecord("teams", col);
                int teamId = (Integer) rec.getFieldValue("id");
                ColumnData[] changes =
                { 
                     new ColumnData( "firstname", firstnameField.getText( ) ),
                     new ColumnData( "lastname", lastnameField.getText( ) ),
                     // new ColumnData( "teamId", teamBox.getSelectedIndex( ) ) 
                     new ColumnData("teamId", teamId)
                };
                
                dao.updateRecord( "coaches", identifiers, changes );
                
                ColumnData[] iden = {new ColumnData("id", teamId)};
                ColumnData[] ch = {new ColumnData("coach", model.getValueAt(selectedRow, 0))};
                dao.updateRecord("teams", iden, ch);
                
                model.fireTableDataChanged( );
                thisDialog.close( );
            }
        } );
        closeButton.addActionListener( new ActionListener( )
        {
            
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                thisDialog.close( );
            }
            
        } );
    }
    
}
